import { createStore } from 'vuex'
import api from '../common/api'
import { setStorage } from '../common/util'
const userInfo = {id:''} 
const selRole : [{ name : string; code : string; pCode : string; pName : string }] | null = null
const userStore = createStore({
	//存放状态
	state: {
		"userInfo": userInfo,
		"selRole": selRole,
		"temporaryToken": ''
	},
	mutations: {
		setUserInfo(state, userInfo : any) {
			state.userInfo = userInfo
			setStorage("userInfo", userInfo);
		},
		logout(state) {
			state.userInfo = {} as any
			setStorage("userInfo", {});
			setStorage("userAuth", {});
		},
		setSelRole(state, selRole : [{ name : string; code : string; pCode : string; pName : string }] | null) {
			state.selRole = selRole
		},
		setTemporaryToken(state, token : string) {
			state.temporaryToken = token
		}
	},
	actions: {
		async getUserInfo() {
			const userInfoRes = await api.getUserInfo()
			this.commit('setUserInfo', userInfoRes.data)
		}
	}
})

export default userStore